
import SQLite

class DbUser : DbBase {
    var user: Table = Table("users")
    let _id = Expression<Int64>("id")
    let _userId = Expression<String>("userId")
    let _name = Expression<String>("name")
    let _portrait = Expression<String>("portrait")
    let _alias = Expression<String>("alias")
    let _friendAlias = Expression<String>("friendAlias")
    let _describes = Expression<String>("describes")
    let _content = Expression<String>("content")
    let _state = Expression<Int>("state")
    let _timestamp = Expression<Int64>("timestamp")
    let _role = Expression<String>("role")
    
    func open(){
        do{
            try getDb()?.run(user.create(ifNotExists: true) { t in
                t.column(_id, primaryKey: .autoincrement)
                t.column(_userId, unique: true)
                t.column(_name)
                t.column(_portrait)
                t.column(_alias)
                t.column(_friendAlias)
                t.column(_describes)
                t.column(_content)
                t.column(_state)
                t.column(_timestamp)
                t.column(_role)
            })
            try getDb()?.run(user.createIndex(_userId, ifNotExists: true))
        }catch{
            print("\(error)")
        }
    }
    func toInfo(_ row:Row)->DMCCUser{
        let u = DMCCUser()
        u.id = row[_id]
        u.userId = row[_userId]
        u.name = row[_name]
        u.portrait = row[_portrait]
        u.alias = row[_alias]
        u.friendAlias = row[_friendAlias]
        u.describes = row[_describes]
        u.content = row[_content]
        u.state = row[_state]
        u.timestamp = row[_timestamp]
        u.role = row[_role]
        return u
    }
    func getUserInfo(_ osnId:String) -> DMCCUser? {
        do{
            for user in try getDb()!.prepare(user.filter(osnId == _userId)) {
                return toInfo(user)
            }
        }catch{
            print("\(error)")
        }
        return nil
    }
    
    func addUserInfo(_ info:DMCCUser) -> Bool {
        do{
            return try getDb()!.run(user.upsert(
                _userId <- info.userId,
                _name <- info.name,
                _portrait <- info.portrait,
                _alias <- info.alias,
                _friendAlias <- info.friendAlias,
                _describes <- info.describes,
                _content <- info.content,
                _state <- info.state,
                _timestamp <- info.timestamp,
                _role <- info.role,
                onConflictOf: _userId)) >= 0
        }catch{
            print("\(error)")
        }
        return false
    }
    func searchUser(_ text:String)->[DMCCUser]{
        var ms:[DMCCUser] = []
        do{
            for user in try getDb()!.prepare(
                user.filter(_friendAlias.like("%"+text+"%") || _alias.like("%"+text+"%") || _name.like("%"+text+"%"))) {
                let u = toInfo(user)
                ms.append(u)
            }
        }catch{
            print("\(error)")
        }
        return ms

    }
}
let dbUser = DbUser()
